{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### OCR日报识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T08:57:41.117794Z",
     "start_time": "2019-10-22T08:57:35.068965Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "哥俩好,但不同\n",
      "\n",
      "很多人第一次听说MCU芯片平台,可能会好奇,它和无剑SoC平台有什么区别?\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "一句话来说,它俩都是SoC平台,是平头哥的“一母同胥“,东都是为同一个使命而生:推出面向MCU、工业、安全、车载等应用领域\n",
      "的SoC平台,帮助容户降低芯片设计门戬。\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "但是,无剑5oC平台的服务对象更偏产业,它可为宰户提供一站式芯片设计服务,包括代理生产、封装、测试等供应链管理服务、芯片产\n",
      "品的蔬地及市场验记服李等等,昏浦盛花片研元“景产“茶地的全链限古桐、\n"
     ]
    }
   ],
   "source": [
    "# 识别前处理\n",
    "# 图片二值化\n",
    "from PIL import Image\n",
    "import os\n",
    "os.chdir('D:\\OCR')\n",
    "img = Image.open('3.png')\n",
    " \n",
    "# 模式L”为灰色图像，它的每个像素用8个bit表示，0表示黑，255表示白，其他数字表示不同的灰度。\n",
    "Img = img.convert('L')\n",
    "Img.save(\"test1.png\")\n",
    " \n",
    "# 自定义灰度界限，大于这个值为黑色，小于这个值为白色\n",
    "threshold = 200\n",
    " \n",
    "table = []\n",
    "for i in range(256):\n",
    "    if i < threshold:\n",
    "        table.append(0)\n",
    "    else:\n",
    "        table.append(1)\n",
    " \n",
    "# 图片二值化\n",
    "photo = Img.point(table, '1')\n",
    "photo.save(\"test2.png\")\n",
    "# 识别图片内容\n",
    "import pytesseract\n",
    "img_path = 'test2.png'\n",
    "\n",
    "text=pytesseract.image_to_string(Image.open(img_path),lang='chi_sim')\n",
    "\n",
    "text = text.replace(' ','')\n",
    "# citys = ['无锡市','淮安市','中山市','东莞市','重庆市辖区','南京市','徐州市','武汉市','成都市','呼和浩特市']\n",
    "# for c in citys:\n",
    "#     text = text.replace('TIANCHENGRONGZU',c,1)\n",
    "with open('text.txt','w+') as f:\n",
    "    f.write(text)\n",
    "print(text)\n",
    "#os.popen(r'C:\\Program Files (x86)\\Notepad++\\notepad++')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
